<template>
  <div class="product-relation custom-scrollbar">
    <el-steps  :active="4" align-center>
      <el-step title="填写商品信息" icon="el-icon-edit"></el-step>
      <el-step title="填写商品属性" icon="el-icon-price-tag"></el-step>
      <el-step title="添加商品图片" icon="el-icon-picture"></el-step>
      <el-step title="选择商品关联" icon="el-icon-connection"></el-step>
    </el-steps>

    <div class="relation-container">
      <!-- 关联专题 -->
      <div class="relation-section">
        <div class="section-title">关联专题：</div>
        <div class="transfer-container">
          <!-- 待选择面板 -->
          <div class="panel">
            <div class="panel-header">
              <el-checkbox v-model="topicAllChecked">待选择</el-checkbox>
              <span class="count">{{ waitingTopics.length }}/7</span>
            </div>
            <div class="search-container">
              <el-input
                v-model="topicSearchKey"
                placeholder="请输入专题名称"
                prefix-icon="el-icon-search"
                clearable
                class="search-input"
              />
            </div>
            <div class="checkbox-list custom-scrollbar">
              <el-checkbox-group v-model="selectedWaitingTopics">
                <el-checkbox 
                  v-for="item in filteredWaitingTopics" 
                  :key="item.id" 
                  :label="item.id"
                  class="checkbox-item">
                  <span class="checkbox-text">{{ item.name }}</span>
                </el-checkbox>
              </el-checkbox-group>
              <div v-if="waitingTopics.length === 0" class="no-data">无数据</div>
            </div>
          </div>

          <!-- 转移按钮容器 -->
          <div class="transfer-buttons-container">
            <div class="transfer-buttons">
              <el-button 
                type="primary"
                circle 
                class="transfer-button"
                :disabled="selectedWaitingTopics.length === 0"
                @click="moveToRight('topic')"
              >
                <i class="el-icon-arrow-right"></i>
              </el-button>
              <el-button 
                type="primary"
                circle 
                class="transfer-button"
                :disabled="selectedChosenTopics.length === 0"
                @click="moveToLeft('topic')"
              >
                <i class="el-icon-arrow-left"></i>
              </el-button>
            </div>
          </div>

          <!-- 已选择面板 -->
          <div class="panel">
            <div class="panel-header">
              <el-checkbox v-model="topicChosenAllChecked">已选择</el-checkbox>
              <span class="count">{{ chosenTopics.length }}/0</span>
            </div>
            <div class="search-container">
              <el-input
                v-model="topicChosenSearchKey"
                placeholder="请输入专题名称"
                prefix-icon="el-icon-search"
                clearable
                class="search-input"
              />
            </div>
            <div class="checkbox-list custom-scrollbar">
              <el-checkbox-group v-model="selectedChosenTopics">
                <el-checkbox 
                  v-for="item in filteredChosenTopics" 
                  :key="item.id" 
                  :label="item.id"
                  class="checkbox-item">
                  {{ item.name }}
                </el-checkbox>
              </el-checkbox-group>
              <div v-if="chosenTopics.length === 0" class="no-data">无数据</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 关联优选 -->
      <div class="relation-section">
        <div class="section-title">关联优选：</div>
        <div class="transfer-container">
          <!-- 待选择面板 -->
          <div class="panel">
            <div class="panel-header">
              <el-checkbox v-model="preferredAllChecked">待选择</el-checkbox>
              <span class="count">{{ waitingPreferred.length }}/4</span>
            </div>
            <el-input
              v-model="preferredSearchKey"
              placeholder="请输入优选名称"
              prefix-icon="el-icon-search"
              clearable
            />
            <div class="checkbox-list">
              <el-checkbox-group v-model="selectedWaitingPreferred">
                <el-checkbox 
                  v-for="item in filteredWaitingPreferred" 
                  :key="item.id" 
                  :label="item.id"
                  class="checkbox-item">
                  {{ item.name }}
                </el-checkbox>
              </el-checkbox-group>
              <div v-if="waitingPreferred.length === 0" class="no-data">无数据</div>
            </div>
          </div>

          <!-- 转移按钮容器 -->
          <div class="transfer-buttons-container">
            <div class="transfer-buttons">
              <el-button 
                type="primary"
                circle 
                size="small"
                icon="el-icon-arrow-right"
                :disabled="selectedWaitingPreferred.length === 0"
                @click="moveToRight('preferred')"
                class="transfer-button"
              ></el-button>
              <el-button 
                type="primary"
                circle 
                size="small"
                icon="el-icon-arrow-left"
                :disabled="selectedChosenPreferred.length === 0"
                @click="moveToLeft('preferred')"
                class="transfer-button"
              ></el-button>
            </div>
          </div>

          <!-- 已选择面板 -->
          <div class="panel">
            <div class="panel-header">
              <el-checkbox v-model="preferredChosenAllChecked">已选择</el-checkbox>
              <span class="count">{{ chosenPreferred.length }}/0</span>
            </div>
            <el-input
              v-model="preferredChosenSearchKey"
              placeholder="请输入优选名称"
              prefix-icon="el-icon-search"
              clearable
            />
            <div class="checkbox-list">
              <el-checkbox-group v-model="selectedChosenPreferred">
                <el-checkbox 
                  v-for="item in filteredChosenPreferred" 
                  :key="item.id" 
                  :label="item.id"
                  class="checkbox-item">
                  {{ item.name }}
                </el-checkbox>
              </el-checkbox-group>
              <div v-if="chosenPreferred.length === 0" class="no-data">无数据</div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="button-container">
      <el-button @click="goBack">上一步，填写商品属性</el-button>
      <el-button type="primary" @click="finish">完成，提交商品</el-button>
    </div>
  </div>
</template>

<style scoped>
.product-relation {
  padding: 30px;
  background: linear-gradient(to bottom, #ffffff, #f8f9fa);
  border-radius: 15px;
  height: calc(100vh - 120px);
  overflow-y: auto;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
}

.page-header {
  text-align: center;
  margin: 30px 0 40px;
}

.page-header h2 {
  color: #2c3e50;
  font-size: 24px;
  margin-bottom: 10px;
}

:deep(.el-step__title.is-process) {
  color: #409EFF;
  font-weight: 600;
}

:deep(.el-step__head.is-process) {
  border-color: #409EFF;
  color: #409EFF;
}

.custom-steps {
  margin-bottom: 40px;
  padding: 20px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

/* 自定义滚动条样式 */
.custom-scrollbar::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background-color: #c0c4cc;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background-color: #909399;
}

.custom-scrollbar::-webkit-scrollbar-track {
  border-radius: 3px;
  background-color: #f5f7fa;
}

.relation-container {
  margin-top: 20px;
}

.relation-section {
  margin-bottom: 30px;
}

.section-title {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 15px;
}

.transfer-container {
  display: flex;
  gap: 20px;
  min-height: 400px;
}

.panel {
  flex: 1;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  background: #fff;
  min-height: 400px;
  max-height: 400px;
}

.panel-header {
  padding: 12px 15px;
  border-bottom: 1px solid #dcdfe6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #f5f7fa;
}

.search-container {
  padding: 10px;
  border-bottom: 1px solid #dcdfe6;
}

.search-input {
  width: 100%;
}

:deep(.el-input__inner) {
  border-radius: 4px;
  height: 32px;
  line-height: 32px;
  padding-left: 30px !important;
  font-size: 14px;
}

:deep(.el-input__inner):hover {
  border-color: #c0c4cc;
}

:deep(.el-input__inner):focus {
  border-color: #409EFF;
}

:deep(.el-input__prefix) {
  left: 8px;
  height: 100%;
  display: flex;
  align-items: center;
}

:deep(.el-input__prefix-inner) {
 display: flex;
 align-items: center;
}

:deep(.el-input__icon) {
 line-height: 1;
 height: 32px;
 width: 14px;
}

:deep(.el-input__icon::before) {
 display: flex;
 align-items: center;
 height: 100%;
}


.checkbox-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
}

.checkbox-item {
  margin: 8px 0;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s;
  width: 100%;
  display: block !important;
}

:deep(.el-checkbox) {
 display: flex !important;
 align-items: flex-start !important;  /* 多行文本时顶部对齐 */
 height: auto !important;
 margin-right: 0 !important;
}

:deep(.el-checkbox__input) {
 margin-top: 3px;  /* 复选框与文本顶部对齐 */
 flex-shrink: 0;  /* 防止复选框被压缩 */
}

:deep(.el-checkbox__label) {
 white-space: normal !important;  /* 允许文本换行 */
 line-height: 1.5;
 padding-left: 8px;  /* 调整文本与复选框的间距 */
 word-break: break-all;  /* 允许在任意字符间换行 */
}

.checkbox-item:hover {
  background-color: #f5f7fa;
}

.count {
  color: #909399;
  font-size: 14px;
}

.transfer-buttons-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 40px;
  padding: 0;
  margin: 0 10px;
}

.transfer-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.transfer-button {
  width: 24px !important;
  height: 24px !important;
  padding: 0 !important;
  display: flex !important;
  align-items: center;
  justify-content: center;
  transition: all 0.3s;
  background-color: #409EFF !important;
  border-color: #409EFF !important;
  margin: 0 !important;
}

.transfer-button i {
  font-size: 12px;
  line-height: 1;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
}

.transfer-button:hover:not(:disabled) {
  transform: scale(1.1);
  background-color: #66b1ff !important;
  border-color: #66b1ff !important;
}

.transfer-button:disabled {
  background-color: #a0cfff !important;
  border-color: #a0cfff !important;
  cursor: not-allowed;
  opacity: 0.7;
}

.no-data {
  text-align: center;
  color: #909399;
  padding: 20px 0;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .transfer-container {
    flex-direction: column;
  }

  .panel {
    width: 100%;
  }

  .transfer-buttons-container {
    flex-direction: row;
    width: 100%;
    height: 40px;
    padding: 0;
    margin: 10px 0;
  }

  .transfer-buttons {
    flex-direction: row;
    gap: 12px;
    height: auto;
  }
}
</style>

<script>
import { mapState,mapActions} from 'vuex';

export default {
  name: 'ProductRelation',
  data() {
    return {
      // 专题相关数据
      topicSearchKey: '',
      topicChosenSearchKey: '',
      topicAllChecked: false,
      topicChosenAllChecked: false,
      selectedWaitingTopics: [],
      selectedChosenTopics: [],
      waitingTopics: [
        { id: 1, name: '大牌手机低价秒' },
        { id: 2, name: '晚龙845新品上市' },
        { id: 3, name: '夏天应该穿什么' },
        { id: 4, name: '夏季精选' },
        { id: 5, name: '品牌手机降价' },
        { id: 6, name: '测试用专题' }
      ],
      chosenTopics: [],

      // 优选相关数据
      preferredSearchKey: '',
      preferredChosenSearchKey: '',
      preferredAllChecked: false,
      preferredChosenAllChecked: false,
      selectedWaitingPreferred: [],
      selectedChosenPreferred: [],
      waitingPreferred: [
        { id: 1, name: '让首页更出众' },
        { id: 2, name: '让首页更出众22' },
        { id: 3, name: '让首页更出众33' },
        { id: 4, name: '让首页更出众44' }
      ],
      chosenPreferred: []
    }
  },
  computed: {
    // 筛选待选专题
    filteredWaitingTopics() {
      return this.waitingTopics.filter(item => 
        item.name.includes(this.topicSearchKey)
      )
    },
    // 筛选已选专题
    filteredChosenTopics() {
      return this.chosenTopics.filter(item => 
        item.name.includes(this.topicChosenSearchKey)
      )
    },
    // 筛选待选优选
    filteredWaitingPreferred() {
      return this.waitingPreferred.filter(item => 
        item.name.includes(this.preferredSearchKey)
      )
    },
    // 筛选已选优选
    filteredChosenPreferred() {
      return this.chosenPreferred.filter(item => 
        item.name.includes(this.preferredChosenSearchKey)
      )
    },
    ...mapState('productForm', ['attribute']),
    relationForm: {
      get() {
        return this.relation
      },
      set(value) {
        this.$store.commit('productForm/UPDATE_RELATION', value)
      }
    }
  },
  //在created钩子中初始化数据
  created(){
    //如果store中已有数据，使用存储的数据
    if(this.relation){
      this.relationForm = this.relation
    }
  },
  methods: {
    // 移动到右边面板
    moveToRight(type) {
      if (type === 'topic') {
        const itemsToMove = this.waitingTopics.filter(item => 
          this.selectedWaitingTopics.includes(item.id)
        )
        this.chosenTopics.push(...itemsToMove)
        this.waitingTopics = this.waitingTopics.filter(item => 
          !this.selectedWaitingTopics.includes(item.id)
        )
        this.selectedWaitingTopics = []
      } else {
        const itemsToMove = this.waitingPreferred.filter(item => 
          this.selectedWaitingPreferred.includes(item.id)
        )
        this.chosenPreferred.push(...itemsToMove)
        this.waitingPreferred = this.waitingPreferred.filter(item => 
          !this.selectedWaitingPreferred.includes(item.id)
        )
        this.selectedWaitingPreferred = []
      }
    },
    // 移动到左边面板
    moveToLeft(type) {
      if (type === 'topic') {
        const itemsToMove = this.chosenTopics.filter(item => 
          this.selectedChosenTopics.includes(item.id)
        )
        this.waitingTopics.unshift(...itemsToMove)
        this.chosenTopics = this.chosenTopics.filter(item => 
          !this.selectedChosenTopics.includes(item.id)
        )
        this.selectedChosenTopics = []
      } else {
        const itemsToMove = this.chosenPreferred.filter(item => 
          this.selectedChosenPreferred.includes(item.id)
        )
        this.waitingPreferred.unshift(...itemsToMove)
        this.chosenPreferred = this.chosenPreferred.filter(item => 
          !this.selectedChosenPreferred.includes(item.id)
        )
        this.selectedChosenPreferred = []
      }
    },
    ...mapActions('productForm', ['submitProduct']),
   
  finish() {
    this.$store.dispatch('productForm/CLEARN_ALL')
    this.$message.success("添加成功")
    this.$router.push('/home/goodsCategory')
   },
   goBack(){
      this.$router.push('/home/addProduct/productAttribute')
   }
  }
}
</script> 